﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using VAdvantage.Model;
using VAdvantage.Logging;
using VAdvantage.Utility;
using System.Text;
using VAdvantage.Grids;
using SLControls.Controls;
using System.Data;
using VAdvantage.Classes;
using System.Threading;
using VAdvantage.DataBase;
using System.Collections;
using VAdvantage.Panels;
using System.ComponentModel;
using SilverlightDesktop.Controls;
using VAdvantage.Apps;

using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System.IO;

using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Collections.ObjectModel;
using VAdvantage.MailBox;
using VAdvantage.MailBox.Classes;
using System.Windows.Browser;


using VFileUploader;
using System.Windows.Data;
using VAdvantage.DataServiceBasic2;
using System.Windows.Media.Imaging;

using System.Reflection;
//using Microsoft.Practices.Prism.ViewModel;


namespace VAdvantage.Framework
{

    public partial class Email : SilverlightDesktop.Controls.Form
    {
       

        ////      public Email()
        ////      {
        ////          InitializeComponent();
        ////      }

        //To show attachment of saved letters
        public Email(Ctx ctx, string title, MMailAttachment1 mailatt)
        // : base(false, true, true, true, title)
        {
            InitializeComponent();

            Type type1 = Envs.GetC1ModuleType("VC1Module.UI.Email");

            if (type1 != null)
            {
                System.Reflection.ConstructorInfo constructor = null;
                try
                {
                    constructor = type1.GetConstructor(new Type[] { typeof(Ctx), typeof(string), typeof(MMailAttachment1),typeof(Form) });
                }
                catch (Exception e)
                {
                    //log.Warning("No transaction Constructor for " + type.FullName + " (" + e.ToString() + ")");
                }
                if (constructor != null)
                {
                    try
                    {

                        object ig = constructor.Invoke(new object[] { ctx, title,mailatt, this});
                        container.Children.Add(ig as UIElement);
                    }
                    catch
                    {

                    }
                }
                else
                {
                    ///throw new Exception("No Std Constructor");
                }
            }
        }

        public Email(Ctx ctx, MMailAttachment1 mailatt)
        {
            InitializeComponent();

            Type type1 = Envs.GetC1ModuleType("VC1Module.UI.Email");

            if (type1 != null)
            {
                System.Reflection.ConstructorInfo constructor = null;
                try
                {
                    constructor = type1.GetConstructor(new Type[] { typeof(Ctx), typeof(MMailAttachment1), typeof(Form) });
                }
                catch (Exception e)
                {
                    //log.Warning("No transaction Constructor for " + type.FullName + " (" + e.ToString() + ")");
                }
                if (constructor != null)
                {
                    try
                    {

                        object ig = constructor.Invoke(new object[] { ctx,mailatt, this});
                        container.Children.Add(ig as UIElement);
                    }
                    catch
                    {

                    }
                }
                else
                {
                    ///throw new Exception("No Std Constructor");
                }
            }

        }

        /// <summary>
        /// Constructor used to upload open email attachment
        /// Open email formate from attachment
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="title"></param> 
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="message"></param>
        /// <param name="mailatt"></param>
        public Email(Ctx ctx, string title, object to, object subject, object message, MMailAttachment1 mailatt, bool type)
        //   : base(false, true, true, true, title)
        {
            InitializeComponent();
           
            Type type1 = Envs.GetC1ModuleType("VC1Module.UI.Email");

            if (type1 != null)
            {
                System.Reflection.ConstructorInfo constructor = null;
                try
                {
                    constructor = type1.GetConstructor(new Type[] { typeof(Ctx), typeof(string), typeof(Object), typeof(object), typeof(object), typeof(MMailAttachment1), typeof(bool),typeof(Form)});
                }
                catch (Exception e)
                {
                    //log.Warning("No transaction Constructor for " + type.FullName + " (" + e.ToString() + ")");
                }
                if (constructor != null)
                {
                    try
                    {

                        object ig = constructor.Invoke(new object[] { ctx, title, to,
                        subject,message,mailatt, type, this});
                        container.Children.Add(ig as UIElement);
                    }
                    catch
                    {

                    }
                }
                else
                {
                    ///throw new Exception("No Std Constructor");
                }
            }

        }

        /// <summary>
        /// Open Email form My Contact window....
        /// </summary>
        /// <param name="emailAddress"></param>
        /// <param name="IDs"></param>
        /// <param name="tableID"></param>
        public Email(List<string> emailAddress, List<int> IDs, int tableID)
        {
            InitializeComponent();
            Type type = Envs.GetC1ModuleType("VC1Module.UI.Email");

            if (type != null)
            {
                System.Reflection.ConstructorInfo constructor = null;
                try
                {
                    constructor = type.GetConstructor(new Type[] { typeof(List<string>), typeof(List<int>), typeof(int), typeof(Form) });
                }
                catch (Exception e)
                {
                    //log.Warning("No transaction Constructor for " + type.FullName + " (" + e.ToString() + ")");
                }
                if (constructor != null)
                {
                    try
                    {

                        object ig = constructor.Invoke(new object[] { emailAddress, IDs, tableID,
                         this});
                        container.Children.Add(ig as UIElement);
                    }
                    catch
                    {

                    }
                }
                else
                {
                   // throw new Exception("No Std Constructor");
                }
            }
        }

        /// <summary>
        /// Constructor Used for Send Email with Save formate
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="title"></param>
        /// <param name="drv"></param>

        // C1SpellChecker spellcheck=null;

        public Email(Ctx ctx, string title, string to, GridTab _curtab, int Record_ID, bool isEmail, GridController _curGC)
        {
            InitializeComponent();
                Assembly asm = null;
                Type type = Envs.GetC1ModuleType("VC1Module.UI.Email");
                
                if (type != null)
                {
                    System.Reflection.ConstructorInfo constructor = null;
                    try
                    {
                        constructor = type.GetConstructor(new Type[] { typeof(Ctx), typeof(string), typeof(string), typeof(GridTab), typeof(int), typeof(bool), typeof(GridController),typeof(Form)});
                    }
                    catch (Exception e)
                    {
                        //log.Warning("No transaction Constructor for " + type.FullName + " (" + e.ToString() + ")");
                    }
                    if (constructor != null)
                    {
                        try
                        {

                           object ig = constructor.Invoke(new object[] { ctx, title, to,
                        _curtab,Record_ID,isEmail, _curGC, this});
                            container.Children.Add(ig as UIElement);
                        }
                        catch
                        { 
                        
                        }
                    }
                    else
                    {
                        ///throw new Exception("No Std Constructor");
                    }
                }

            }

        public Email(Ctx ctx, string title, string to, GridTab _curtab, int Record_ID, GridController _curGC)
        {
            InitializeComponent();
            Assembly asm = null;
            Type type = Envs.GetC1ModuleType("VC1Module.UI.Email");

            if (type != null)
            {
                System.Reflection.ConstructorInfo constructor = null;
                try
                {
                    constructor = type.GetConstructor(new Type[] { typeof(Ctx), typeof(string), typeof(string), typeof(GridTab), typeof(int),  typeof(GridController), typeof(Form) });
                }
                catch (Exception e)
                {
                    //log.Warning("No transaction Constructor for " + type.FullName + " (" + e.ToString() + ")");
                }
                if (constructor != null)
                {
                    try
                    {

                        object ig = constructor.Invoke(new object[] { ctx, title, to,
                        _curtab,Record_ID, _curGC, this});
                        container.Children.Add(ig as UIElement);
                    }
                    catch
                    {

                    }
                }
                else
                {
                    ///throw new Exception("No Std Constructor");
                }
            }




        }

        #region VC1Panel Members

        public Grid ContainerGrid
        {
            get
            {
                return this.container;
            }
        }

        #endregion
    }

    public class AttachmentsInfo
    {
        public byte[] Buffer { get; set; }
        public string FileName { get; set; }
    }
}
